﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net.Mail;
using System.Threading;

namespace Email
{
    class Program
    {
        private static TimeSpan waitTime = new TimeSpan(1, 0, 0);

        private static string username = "catfactsssforyou@gmail.com";
        private static string password = "oldspice1337";

        private static List<string> emailList = new List<string>();
        private static List<string> factsList = new List<string>();

        private static string[] facts = { "The domestic house cat is a small carnivorous mammal. Its most immediate ancestor is believed to be the African wild cat. The cat has been living in close association with humans for somewhere between 3,500 and 8,000 years.",
                                   "Cats usually weigh between 2.5 and 7 kg (5.5–16 pounds), although some breeds can exceed 11.3 kg (25 pounds).",
                                 "Indoor cats typically live 14 to 20 years although the oldest-known cat lived to an amazing age of 36.", 
                                 "Spaying and neutering cats also decrease the risk of testicular and ovarian cancer.",
                                 "A common belief says that cats always land on their feet; well they usually do - but not always.",
                                   "Cats, especially young kittens, are known for their love of string play.",
                                   "Most cats can't resist a dangling piece of string, or a piece of rope drawn randomly and enticingly across the floor.",
                                 "Cats have highly specialized teeth and a digestive system suitable for eating meat.",
                                 "The cat's tongue has sharp spines,  papillae, useful for retaining and ripping flesh from a carcass.",
                                 "Cats, including the great cats, have a genetic anomaly that prevents them from tasting sweetness.",
                                 "Cats may have many periods of heat over the course of a year. A heat period lasts about 4 to 7 days if the female is bred; if she is not bred, the heat period lasts longer.",
                                 "Cats are capable of as many as 100 different vocalizations, compared to about 10 for dogs."};


        static void Main(string[] args)
        {
            emailList.Add("6135583236@msg.telus.com");
            emailList.Add("6138633203@txt.bell.ca");
            factsList.AddRange(facts);

            Thread t = new Thread(start);
            t.Start();

            string cmd;
            do
            {
                cmd = Console.ReadLine();

                if (cmd.StartsWith("/f a "))
                {
                    string email = cmd.Remove(0, 5);
                    emailList.Add(email);
                }
                else if (cmd.StartsWith("/f r "))
                {
                    string email = cmd.Remove(0, 5);
                    emailList.Remove(email);
                }
                else if (cmd.StartsWith("/f l "))
                {
                    foreach (string s in emailList)
                        Console.WriteLine(s);
                }
                else if (cmd.StartsWith("/l a "))
                {
                    string fact = cmd.Remove(0, 5);
                    factsList.Add(fact);
                }
                else if (cmd.StartsWith("/f m "))
                {
                    string msg = cmd.Remove(0, 5);
                    broadcast(msg);
                }
            } while (1 == 1);
        }

        private static void start()
        {
            SmtpClient gmail = new SmtpClient("smtp.gmail.com", 587);
            gmail.DeliveryMethod = SmtpDeliveryMethod.Network;
            gmail.UseDefaultCredentials = false;
            gmail.EnableSsl = true;
            gmail.Credentials = new System.Net.NetworkCredential(username, password);

            do
            {
                Console.WriteLine("sending");
                try
                {
                    Random rnd = new Random();
                    string msg = factsList[rnd.Next(facts.Length)];

                    string[] who = emailList.ToArray();

                    MailMessage message = new MailMessage(username, String.Join(",", who), "Cat Fact!", msg);
                    gmail.Send(message);
                }
                catch (Exception e)
                {
                    Console.WriteLine(e.Message);
                }

                Console.WriteLine("sleeping...");
                Thread.Sleep(waitTime);
            } while (1 == 1);
        }

        private static void broadcast(string msg)
        {
            try
            {
                SmtpClient gmail = new SmtpClient("smtp.gmail.com", 587);
                gmail.DeliveryMethod = SmtpDeliveryMethod.Network;
                gmail.UseDefaultCredentials = false;
                gmail.EnableSsl = true;
                gmail.Credentials = new System.Net.NetworkCredential(username, password);

                string[] who = emailList.ToArray();

                MailMessage message = new MailMessage(username, String.Join(",", who), "Cat Fact!", msg);
                gmail.Send(message);
            }
            catch (Exception e)
            {
                Console.WriteLine(e.Message);
            }
        }

    }
}
